//
//  FiltrateViewController.m
//  觅影
//
//  Created by Zeus on 16/4/13.
//  Copyright © 2016年  陶虹. All rights reserved.
//

#import "FiltrateViewController.h"
#import "PopSoonCollectionViewCell.h"
#import "HotModel.h"
#import "LibraryDetailViewController.h"

@interface FiltrateViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *filtrateCollectionView;
@property (nonatomic, assign) CGFloat filtrateWidth;
@property (nonatomic, strong) NSMutableArray *filtrateArr;
@property (nonatomic, assign) NSInteger page;
@end

@implementation FiltrateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = _classifyTitle;
    UICollectionViewFlowLayout *filtrateLayout = (UICollectionViewFlowLayout *)self.filtrateCollectionView.collectionViewLayout;
    self.filtrateWidth = (KWIDTH - 8*5) / 3;
    // 设置item
    filtrateLayout.itemSize = CGSizeMake(self.filtrateWidth, self.filtrateWidth*1.4);
    filtrateLayout.minimumInteritemSpacing = 3;
    filtrateLayout.minimumLineSpacing = 3;
    self.filtrateCollectionView.delegate = self;
    self.filtrateCollectionView.dataSource = self;
    [self.filtrateCollectionView registerNib:[UINib nibWithNibName:@"PopSoonCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"popSoon"];
    [self searchFiltrateMoviesData];
    self.filtrateCollectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadMoreData];
    }];
}
#pragma mark --- 加载更多数据 ---
- (void)loadMoreData{
    _page++;
    [RequestManager requestWithUrl:[NSString stringWithFormat:@"http://static.v6v7.com/pingping_japi/film/applist?filmtypeId=%ld&page=%ld&page_size=12", [self.classifyId integerValue], self.page] requestType:RequestTypeGET parDic:nil finshed:^(NSData *data) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary *resultDic = dic[@"result"];
        NSMutableArray *movieListArray = resultDic[@"movieList"];
        for (NSMutableDictionary *movieListDic in movieListArray) {
            HotModel *model = [[HotModel alloc]init];
            [model setValuesForKeysWithDictionary:movieListDic];
            [self.filtrateArr addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.filtrateCollectionView reloadData];
            [self.filtrateCollectionView.mj_footer endRefreshing];
        });
    } error:nil];
}
#pragma mark --- 请求数据 ---
- (void)searchFiltrateMoviesData{
    [RequestManager requestWithUrl:[NSString stringWithFormat:@"http://static.v6v7.com/pingping_japi/film/applist?filmtypeId=%ld&page=1&page_size=12", [self.classifyId integerValue]] requestType:RequestTypeGET parDic:nil finshed:^(NSData *data) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary *resultDic = dic[@"result"];
        NSMutableArray *movieListArray = resultDic[@"movieList"];
        self.filtrateArr = [NSMutableArray array];
        self.page = 1;
        for (NSMutableDictionary *movieListDic in movieListArray) {
            HotModel *model = [[HotModel alloc]init];
            [model setValuesForKeysWithDictionary:movieListDic];
            [self.filtrateArr addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.filtrateCollectionView reloadData];
        });
    } error:nil];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.filtrateArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PopSoonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"popSoon" forIndexPath:indexPath];
    HotModel *model = self.filtrateArr[indexPath.row];
    cell.movieTitle.text = model.title;
    [cell.pictureView sd_setImageWithURL:[NSURL URLWithString:model.poster]];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LibraryDetailViewController *libraryDeatilVc = [[LibraryDetailViewController alloc]initWithNibName:@"LibraryDetailViewController" bundle:nil];
    HotModel *model = self.filtrateArr[indexPath.row];
    libraryDeatilVc.movieId = [model.Id stringValue];
    libraryDeatilVc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:libraryDeatilVc animated:YES];
}
# pragma mark --- 返回集合视图整体的上、左、下、右 ---
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 8, 5, 8);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
